package com.lili.collection.file.plugins;

import com.lili.collection.core.context.BatchRecordContext;
import com.lili.collection.core.context.StageContext;
import com.lili.collection.core.context.impl.GenericBatchRecordContext;
import com.lili.collection.core.error.InputRunnerException;
import com.lili.collection.core.error.StageRecordException;
import com.lili.collection.core.fields.Field;
import com.lili.collection.core.plugins.InputPlugin;
import com.lili.collection.core.record.impl.CommonRecord;
import com.lili.collection.core.runner.result.RunnerResult;
import com.lili.collection.file.base.FileRecordInfo;
import com.lili.collection.file.config.FileInputConfig;
import com.lili.collection.file.handle.FileHandleUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 *描述:
 *@date:2023/3/16  20:28
 *@author:lili
 */
@Slf4j
public class FileInputPlugin implements InputPlugin<FileInputConfig> {

    private String fileName;
    private byte[] bytes;
    //别名配置
    private Map<String,String> aliasMap;

    private RunnerResult fileInputRunnerResult;

    private FileInputConfig config;
    private Throwable throwable;

    @Override
    public void setRunnerResult(RunnerResult result) {
        this.fileInputRunnerResult=result;
    }

    @Override
    public StageContext execute() {
        BatchRecordContext recordContext = new GenericBatchRecordContext();
        FileRecordInfo recordInfo = null;
        try {
            recordInfo = FileHandleUtil.parseFile(this.fileName, this.bytes,this.aliasMap,this.config);
            if(CollectionUtils.isNotEmpty(recordInfo.getSuccessRecord())){
                for (Map<String, Field> rowData : recordInfo.getSuccessRecord()) {
                    recordContext.setRecord(new CommonRecord(Field.create(rowData)));
                }
            }
        } catch (Throwable e) {
            this.throwable=e;
            recordInfo.setSuccessed(false);
            recordContext.setStageRecordState(StageContext.StageRecordState.ALL_ERROR);
            recordContext.setException(new StageRecordException(e));
            log.error("解析文件错误!",e);
        }
        if (this.throwable==null) {
            recordContext.setStageRecordState(StageContext.StageRecordState.ALL_SUCCESS);
            recordContext.setStageContextState(StageContext.StageContextState.COMPLETE);
            if(recordInfo!=null){
                fileInputRunnerResult.setSuccessRecordCount(recordInfo.getSuccessRecordCount());
                fileInputRunnerResult.setTotalRecordCount(recordInfo.getTotalRecordCount());
                fileInputRunnerResult.setErrorRecordCount(recordInfo.getErrorRecordCount());
            }
            log.info("-->数据全部读取完成-->总数据：{}，成功数：{}",
                    recordInfo.getTotalRecordCount(),
                    recordInfo.getSuccessRecordCount());
        }else {
            if(recordInfo!=null){
                recordInfo.setSuccessed(false);
                fileInputRunnerResult.setErrOutputData(recordInfo.getErrOutputData());
            }
        }
        return recordContext;
    }

    @Override
    public void init(FileInputConfig config) {
        try {
            this.config=config;
            this.fileName = config.getFileName();
            this.bytes = config.getBytes();
            if(StringUtils.isBlank(this.fileName)){
                throw new IllegalArgumentException("文件名不能为空!");
            }
            if(ArrayUtils.isEmpty(this.bytes)){
                throw new IllegalArgumentException("需要解析的文件内容不能为空!");
            }
            this.aliasMap=config.getAliasMap();
            if(MapUtils.isEmpty(this.aliasMap)){
                throw new IllegalArgumentException("文件列配置映射字段为空!");
            }
        } catch (Throwable throwable) {
            throw new InputRunnerException(throwable);
        }
    }

    @Override
    public void dispose() {
    }
}